package com.xiao.sulingother.controller.my_oss;

import com.xiao.sulingother.feign.UserFeign;
import com.xiao.sulingpublic.to.MemberTo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @author: 1322127758@qq.com
 * @date: 2022/4/3 11:14
 * @作用: 接收文件
 */
@RequestMapping("/myoss")
@RestController
public class RestImageController {
    @Autowired
    private ThreadPoolExecutor executor;

    @Autowired
    private UserFeign userFeign;

    /**
     * 访问nginx静态文件的路径
     */
    @Value("${spring.my.nginx.url}")
    private String url;

    /**
     * 上传过来的用户头像
     * @param files   :传过来的文件
     * @param userName:传过来的用户
     * @return : 返回图片访问的路径
     */
    @RequestMapping("/restImage")
    public String restImage(@RequestParam("file") MultipartFile [] files, String userName) throws Exception {

        MultipartFile file = files[0];
        //保存文件到

        String originalFilename = file.getOriginalFilename();
        String replace = UUID.randomUUID().toString().replace("-", "");
        String picturePath = "/root/docker/nginx/html/suling/userImg";

        CompletableFuture.runAsync(()->{
            String text = picturePath + "/" + replace + "-" + originalFilename;

            InputStream input = null;
            try {
                input = file.getInputStream();

                //定义byte变量
                int index = 0;

                //每次写入1M
                byte[] bytes = new byte[1024 * 1024];
                FileOutputStream downloadFile = new FileOutputStream(text);
                while ((index = input.read(bytes)) != -1) {
                    //写入
                    downloadFile.write(bytes, 0, index);
                    //刷新一下
                    downloadFile.flush();
                }

                //关闭流
                downloadFile.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        },executor);


        // 头像访问的路径
        String userUrl = url + "suling/userImg/" + replace + "-" + originalFilename;

        CompletableFuture.runAsync(()->{
            // 远程保存头像的访问的路径
            MemberTo memberTo = new MemberTo();
            memberTo.setUsername(userName);
            memberTo.setHeader(userUrl);
            try {
                userFeign.updateByUsername(memberTo);
            } catch (Exception e) {
                e.printStackTrace();
            }
        },executor);

        //返回 图片的访问路径
        // 返回图片访问的;路径
        return userUrl;
    }
}
